<template>
  <div
    ref="custom-table"
    class="custom-table-container"
    :class="{ 'vab-fullscreen': isFullscreen }"
  >
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form
          ref="form"
          :inline="true"
          label-width="80px"
          :model="queryForm"
          @submit.native.prevent
        >
          <el-form-item
            label="车牌号"
            label-width="55px"
            prop="condition.plateNo"
          >
            <el-input
              v-model="queryForm.condition.plateNo"
              clearable
              placeholder="请输入车牌号"
            />
          </el-form-item>
          <el-form-item label="小区编码" prop="condition.areaNo">
            <el-select
              v-model="queryForm.condition.areaNo"
              clearable
              placeholder="请输入小区编码"
            >
              <el-option
                v-for="item in cellCode"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="抓拍日期" prop="queryDate">
            <el-date-picker
              v-model="queryForm.queryDate"
              clearable
              end-placeholder="结束日期"
              range-separator="至"
              start-placeholder="开始日期"
              type="daterange"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-search"
              native-type="submit"
              type="primary"
              @click="handleQuery"
            >
              查询
            </el-button>
            <el-button icon="el-icon-refresh-left" @click="resetQuery">
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
      <vab-query-form-left-panel :lg="0" :md="0" :sm="0" :xl="0" :xs="0" />
      <vab-query-form-right-panel :lg="24" :md="24" :sm="24" :xl="24" :xs="24">
        <el-button
          style="margin: 0 10px 10px 0 !important"
          type="primary"
          @click="clickFullScreen"
        >
          <vab-icon
            :icon="isFullscreen ? 'fullscreen-exit-fill' : 'fullscreen-fill'"
          />
          表格全屏
        </el-button>
        <el-popover
          ref="popover"
          popper-class="custom-table-checkbox"
          trigger="hover"
        >
          <el-radio-group v-model="lineHeight">
            <el-radio label="medium">大</el-radio>
            <el-radio label="small">中</el-radio>
            <el-radio label="mini">小</el-radio>
          </el-radio-group>
          <template #reference>
            <el-button style="margin: 0 10px 10px 0 !important" type="primary">
              <vab-icon icon="line-height" />
              表格尺寸
            </el-button>
          </template>
        </el-popover>
        <el-popover popper-class="custom-table-checkbox" trigger="hover">
          <el-checkbox-group v-model="checkList">
            <vab-draggable v-bind="dragOptions" :list="columns">
              <div v-for="(item, index) in columns" :key="item + index">
                <vab-icon icon="drag-drop-line" />
                <el-checkbox
                  :disabled="item.disableCheck === true"
                  :label="item.label"
                >
                  {{ item.label }}
                </el-checkbox>
              </div>
            </vab-draggable>
          </el-checkbox-group>
          <template #reference>
            <el-button
              icon="el-icon-setting"
              style="margin: 0 0 10px 0 !important"
              type="primary"
            >
              可拖拽列设置
            </el-button>
          </template>
        </el-popover>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :border="border"
      :data="list"
      :size="lineHeight"
      :stripe="stripe"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        label="序号"
        show-overflow-tooltip
        width="95"
      >
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :width="item.width"
      >
        <template #default="{ row }">
          <span v-if="item.label === '行驶方向'">
            {{ row[item.prop] ? (row[item.prop] === '1' ? '入' : '出') : '-' }}
          </span>
          <span v-else>{{ row[item.prop] || '-' }}</span>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { getCarRecord } from '@/api/record'
  import { picBaseUrl } from '@/config'
  import VabDraggable from 'vuedraggable'
  import { mapGetters } from 'vuex'

  export default {
    name: 'CarLog',
    components: {
      VabDraggable,
    },
    data() {
      return {
        baseUrl: picBaseUrl,
        isFullscreen: false,
        border: true,
        fold: true,
        height: this.$baseTableHeight(1),
        stripe: false,
        lineHeight: 'medium',
        checkList: ['车牌号', '行驶方向', '抓拍时间', '设备编码', '小区名称'],
        columns: [
          {
            label: '车牌号',
            width: 'auto',
            prop: 'plateNo',
            sortable: true,
            disableCheck: true,
          },
          {
            label: '行驶方向',
            width: 'auto',
            prop: 'driveDirection',
            sortable: true,
          },
          {
            label: '抓拍时间',
            width: 'auto',
            prop: 'occurDate',
            sortable: true,
          },
          {
            label: '设备编码',
            width: 'auto',
            prop: 'deviceNo',
            sortable: true,
          },
          {
            label: '小区名称',
            width: 'auto',
            prop: 'areaName',
            sortable: true,
          },
        ],
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          condition: {
            orderBy: 'id',
            orderDirect: 'desc',
            plateNo: null,
            areaNo: null,
            startDate: null,
            endDate: null,
          },
          pageNum: 1,
          pageSize: 10,
          queryDate: [],
        },
      }
    },
    computed: {
      ...mapGetters({
        cellCode: 'dict/cellCode',
      }),
      dragOptions() {
        return {
          animation: 600,
          group: 'description',
        }
      },
      finallyColumns() {
        return this.columns.filter((item) =>
          this.checkList.includes(item.label)
        )
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      clickFullScreen() {
        this.isFullscreen = !this.isFullscreen
        this.handleHeight()
      },
      handleFold() {
        this.fold = !this.fold
        this.handleHeight()
      },
      handleHeight() {
        if (this.isFullscreen) this.height = this.$baseTableHeight(1) + 210
        else this.height = this.$baseTableHeight(1)
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        try {
          const param = {
            pageNum: this.queryForm.pageNum,
            pageSize: this.queryForm.pageSize,
            condition: {},
          }
          Object.keys(this.queryForm.condition).forEach((k) => {
            if (this.queryForm.condition[k]) {
              param.condition[k] = this.queryForm.condition[k]
            }
          })
          const queryDate = this.queryForm.queryDate
          if (Array.isArray(queryDate) && queryDate.length === 2) {
            param.condition.startDate = queryDate[0]
            param.condition.endDate = queryDate[1]
          }
          const res = await getCarRecord(param)
          if (res && res.data) {
            const { list, total } = res.data
            this.list = list || []
            this.imageList = []
            this.total = total
          }
        } catch (e) {
          console.log(e)
        }
        this.listLoading = false
      },
      resetQuery() {
        this.$refs['form'].resetFields()
        this.queryForm.pageNum = 1
        this.fetchData()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .custom-table-container {
    ::v-deep {
      i {
        cursor: pointer;
      }
    }

    .stripe-panel,
    .border-panel {
      margin: 0 10px $base-margin/2 10px !important;
    }
  }
</style>
<style lang="scss">
  .custom-table-checkbox {
    [class*='ri'] {
      vertical-align: -2.5px;
      cursor: pointer;
    }

    .el-checkbox {
      margin: 5px 0 5px 8px;
    }
  }
</style>
